// -*- mode: Scala;-*- 
// Filename:    reduce.scala 
// Authors:     lgm                                                    
// Creation:    Fri May  8 18:43:01 2009 
// Copyright:   Not supplied 
// Description: 
// ------------------------------------------------------------------------

package com.biosimilarity.reflection.paratrope.Eval

trait Reductions 
  extends Generators {
    case class Reduction(
      source : Agent, 
      target : Agent,
      residual : Process
    )

    abstract class CandidatesDeReducibility(
      stream : Stream[Reduction]
    ) {
      def choose () : Reduction
    }

    def findCandidates(
      composition : Process
    ) : CandidatesDeReducibility

    def substitute(
      body : Process,
      actuals : List[Process], 
      formals : List[Name]
    ) : Process
    
    def compose( p : Process, q : Process )
    def body( a : Agent ) : Process
    def formals( a : Agent ) : List[Name]
    def actuals( a : Agent ) : List[Process]

    def reduce( p : Process ) = {
      p match {
	case Tangle( n ) => Tangle( n )
	case Transcription( x ) => Transcription( x )
	case Composition( List() ) => Stop
	case Composition( ps ) =>
	  findCandidates( Composition( ps ) ).choose match {
	    case Reduction( source, target, psPrime ) =>
	      compose(
		substitute(
		  body( source ),
		  actuals( target ),
		  formals( source )
		),
		psPrime
	      )
	  }
      }
    }
}
